<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <style>
        #container {
            width: 500px;
            margin: 10px auto;
        }
        form {
            height: 260px;
            overflow: hidden;
        }
        .input {
            margin: 20px 0;
            width: 460px;
            height: 40px;
        }
        .input>label {
            display: inline-block;
            width: 180px;
            text-align: right;
        }
        .input>img {
            width: 150px;
            vertical-align: middle;
        }
        form+div {
            margin-top: 20px;
        }
        a {
            text-decoration: none;
            color: darkcyan;
            font-size: 1.2em;
        }
        .button {
            width: 500px;
            text-align: center;
            margin: 20px 0;
        }
        p.error {
            font-size: 14px;
            color: red;
            height: 16px;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1>用户登录</h1>
        <hr>
        <form @submit.prevent="login()">
            <fieldset>
                <p class="error">{{ hint }}</p>
                <legend>登录信息</legend>
                <div class="input">
                    <label>用户名：</label>
                    <input type="text" v-model.trim="username">
                </div>
                <div class="input">
                    <label>密码：</label>
                    <input type="password" v-model="password">
                </div>
            </fieldset>
            <div class="button">
                <input type="submit" value="登录">
                <input type="reset" value="重置">
            </div>
        </form>
        <div>
            <a href="/">返回首页</a>
            <a href="/register/">注册新用户</a>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
    <script>
        let app = new Vue({
            el: '#container',
            data: {
                hint: '',
                username: '',
                password: ''
            },
            methods: {
                login() {
                    fetch('/login/', {
                        method: 'POST',
                        body: JSON.stringify({
                            username: this.username,
                            password: this.password
                        }),
                        headers: {
                            'content-type': 'application/json',
                        }
                    }).then(resp => resp.json()).then(json => {
                        if (json.code === 40000) {
                            localStorage.token = json.token
                            localStorage.username = json.username
                            location.href = '/'
                        } else {
                            this.hint = json.hint
                        }
                    })
                }
            }
        })
    </script>
</body>
</html>